package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"image"
	"image/color"
	"image/png"
	"math"
	"os"
	"pure/app/controller"
	"pure/app/utils"
	a "pure/astar"
)

func removeConsecutiveDuplicates(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}

	var result []int
	var prev int
	var count int
	consecutive := false

	for i := 0; i < len(arr); i++ {
		if i > 0 && arr[i] == arr[i-1] {
			count++
			if !consecutive {
				prev = arr[i]
				consecutive = true
			}
		} else {
			if consecutive && count >= 2 {
				// 不添加连续的重复元素到结果中
			} else {
				result = append(result, prev)
			}
			consecutive = false
			count = 1
		}
	}

	// 添加最后一个非连续重复的元素
	if !consecutive || count < 2 {
		result = append(result, prev)
	}

	return result
}

var Db *gorm.DB

func InitDb(dsn string) (err error) {
	//dsn := "root:123456@tcp(localhost:3306)/wzdb?charset=utf8mb4&parseTime=True&loc=Local"
	Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	controller.Db = Db
	if err != nil {
		return err
	} else {
		return nil
	}
}
func main() {
	//读取C:\Users\chun\Desktop\Go\pure\public\json\testLine.json
	//解析json

	//sss()
	//fmt.Println(utils.FindClosestFraction(0.5, 10))
	//arr := []int{1, 2, 3, 4, 1, 9, 1, 9, 1, 9, 1, 9, 2}
	//fmt.Println(arr[1:4])
	//fmt.Println(removeConsecutiveDuplicates([]int{1, 2, 3, 4, 1, 9, 1, 9, 1, 9, 1, 9, 2}))
	//fmt.Println(math.Atan2(3, 4) * 180 / math.Pi)
	//angle := math.Atan2(-1, 0) * 180 / math.Pi
	//if angle < 0 {
	//	angle += 360
	//}
	//fmt.Println(angle)
	//Three(119, 38, 0.05, 100)
	//fmt.Println(666)

	err := InitDb("root:123456@tcp(localhost:3306)/geo?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	}

	_ = Db.AutoMigrate(&controller.PointShip{})

	router := gin.Default()

	router.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	astar := router.Group("/star")
	{
		astar.POST("/base", controller.Astar.BaseCh)
		// astar.POST("/lpa", controller.LPAstar.Base)
		astar.POST("/ht", controller.Ht.HtAStar)
		//astar.POST("/ht", _chan.Ht.HtAStar)
		astar.POST("/a", a.Ht.HtAStar)
	}
	base := router.Group("/base")
	{
		base.POST("/outpng", controller.BaseNetObj.Outpng)
		//	OutJson
		base.POST("/outjson", controller.BaseNetObj.OutJson)
	}
	qs := router.Group("/qs")
	{
		qs.POST("/test", controller.Qs.Test)
	}
	line := router.Group("/line")
	{
		//GetLineData
		line.GET("/getLineData", controller.Line.GetLineData)
		//	GetNorthid
		line.GET("/getNorthid", controller.Line.GetNorthid)
	}
	router.Run(":1009")
}

func Three(minLng, maxLat, step float64, precision float64) {
	//landM := make(map[[2]int]struct{})
	f1, _ := os.Open("./public/image/buffer_tu.png")

	num := 0

	defer f1.Close()
	img1, _, _ := image.Decode(f1)

	maxX, maxY := img1.Bounds().Max.X, img1.Bounds().Max.Y
	f, _ := os.Create("./public/image/buffer_tu.txt")

	for i := 0; i <= maxX; i++ {
		for j := 0; j <= maxY; j++ {
			lng := float64(i)*0.01 + minLng
			lat := maxLat - float64(j)*0.01
			//创建一个txt文件，写入内容；根目录为 ./public/image/0.05/
			r, g, b, _ := img1.At(i, j).RGBA()
			if r == 0 && g == 0 && b == 0 {
				continue
			} else {
				f.WriteString(utils.Float64ToStr(math.Round(lat*precision)) + "," + utils.Float64ToStr(math.Round(lng*precision)) + "\n")
				num++
			}
		}
	}

	f.Close()

	fmt.Println(num)
}

func sss() {
	f1, _ := os.Open("./public/image/yh.png")
	defer f1.Close()
	img1, _, _ := image.Decode(f1)
	img1.Bounds()
	imgRgb := img1.(*image.NRGBA)
	//maxLat := 38.0
	//minLng := 119.0

	//创建一个1000*1000的图片
	var imgNew image.Image = image.NewNRGBA(image.Rect(0, 0, 10000, 10000))
	imgNewRgba := imgNew.(*image.NRGBA)
	for i := 5000; i < 15000; i++ {
		for j := 18000; j < 28000; j++ {
			r, g, b, _ := imgRgb.At(i, j).RGBA()
			if r == 0 && g == 0 && b == 0 {
				continue
			} else {
				latY := j - 18000
				lngX := i - 5000
				//imgNew.Set(int(lngX), int(latY), color.RGBA{R: 255, A: 255})
				imgNewRgba.Set(int(lngX), int(latY), color.RGBA{R: 0, A: 255, G: 0, B: 0})
			}
		}
	}

	out, err := os.Create("./public/image/yh33.png")
	if err != nil {
		panic(err)
	}
	defer out.Close()
	png.Encode(out, imgNewRgba)

}
